<?php

namespace MeetUp\Controllers;

use Illuminate\Http\Request;
use MeetUp\Model\Activity;
use MeetUp\Model\ActivityCollect;
use MeetUp\Model\Category;
use MeetUp\Model\Group;
use MeetUp\Model\Tag;
use MeetUp\Model\Topic;
use MeetUp\Model\TopicComment;
use MeetUp\Model\TopicLike;
use MeetUp\Model\UserActivity;
use MeetUp\Model\UserGroup;
use MeetUp\Model\users;


class IndexController extends Controller
{

    //检验用户是否选择分类
    public function tag(Request $request){
        $tag = new Tag();
        $uid = $request->all();
        $result = $tag->where(['tag_uid'=>$uid['uid']])->first();
        if($result !== null){
            $data['state'] = $result->tag_state;
            $data['content'] = $result->tag_content;
            return $this->response($data,200);
        }else{
            return $this->response(['state'=>2],200);
        }
    }
    //用户选择分类
    public function add_tag(Request $request){
        $result = $request->all();$tag = new Tag();
        if(!empty($result['tag_uid'])  && ($result['tag_state'] !== '')){
            $flag = $tag->create($result);
        }else{
            return $this->response([],404,'参数有误');
        }
        if($flag == true){
            return $this->response([],200,'选择分类成功');
        }else{
            return $this->response([],500,'服务器出错');
        }

    }

    //首页热门帖子
    /*public function index(){
        $topic = new topic();
        //if($categoryid == ''){
            $data = $topic->where(['is_hot'=>1,'close'=>1])->orderBy('updated_at','desc')->limit(3)->get()->toArray();
        //}else{
           // $categoryid = explode(',',$categoryid);

        //}

        if($data){
            foreach ($data as $key => $v){
                $data[$key]['topic_img'] = $this->url.$v['topic_img'];
            }
            return $this->response($data,200);
        }else{
            return $this->response([],404,'找不到有关的热门');
        }
    }*/
    public function index(Request $request){
        $topic = new topic();$tag = new Tag();
        $result = $request->all();
        $tag = $tag->where(['tag_uid'=>$result['uid']])->select('tag_content')->first()->toArray();
        //判断用户是否有选择分类
        if($tag['tag_content'] == ''){
        //没有选择分类的热门帖子
        $data = $topic->topic();
            if($data){
                foreach ($data as $key => $value){
                    $data[$key]['topic_img'] = $this->url.$value['topic_img'];
                }
                return $this->response($data,200);
            }else{
                return $this->response([],404,'找不到有关的热门');
            }
        }else{
        //有选择分类查找相应分类下的热帖
        $categoryid = explode(',',$tag['tag_content']);
            foreach ($categoryid as $key => $value){
                $data[$key] = $topic->where(['topic_groupid'=>$value,'is_hot'=>1,'close'=>1])->get()->toArray();
                }
                //dd($data);
            //分类下有帖子但不够三条
            /*if(count($data[0]) == 1){
                $result = $topic->where()
            }elseif (count($data[0]) == 2){

            }*/
                //$data[] = $topic->where(['topic_groupid'=>$value,'is_hot'=>1,'close'=>1])->get()->toArray();
            }
            //合并数组
            $nin = [];
            foreach($data as $val){
                foreach ($val as $k){
                    $nin[] = $k;
                }
            }
            //有选择分类的热门帖子
            if(!empty($nin)){
                foreach ($nin as $key => $value){
                    $nin[$key]['topic_img'] = $this->url.$value['topic_img'];
                }
                return $this->response($nin,200);
            }else{
                //有选择分类但该分类下没有热门帖子
                $data = $topic->topic();
                if($data){
                    foreach ($data as $key => $value){
                        $data[$key]['topic_img'] = $this->url.$value['topic_img'];
                    }
                    return $this->response($data,200);
                }else{
                    return $this->response([],404,'找不到有关的热门');
                }
            }
        }
    //全部帖子
    public function topic(){
        $TopicComment = new TopicComment();
        $TopicLike = new TopicLike();
        //首页全部帖子
        $data = Topic::paginate(10)->toArray();
        if($data){
            foreach ($data['data'] as $key => $value){
                //发帖人信息
                $userinfo = Topic::find($value['topic_id'])->user()->get()->take(5)->toArray();
                $data['data'][$key]['user_id'] = $userinfo[0]['id'];
                $data['data'][$key]['user_name'] = $userinfo[0]['name'];
                $data['data'][$key]['thumimg'] = $userinfo[0]['thumimg'];
                //评论数量
                $data['data'][$key]['comment_count'] = $TopicComment->number($value['topic_id']);
                //点赞数量
                $data['data'][$key]['topic_count'] = $TopicLike->number($value['topic_id']);
                //距发帖的时间差(--分钟前)
                $data['data'][$key]['time'] = $this->formatDate($value['created_at']);
                //图片路径转换
                $data['data'][$key]['topic_img'] = $this->url.$value['topic_img'];

            }
            return $this->response($data);
        }else{
            return $this->response([],404,'稍后再试');
        }
    }
    //帖子的详情
    public function topic_details(Request $request)
    {
        $result = $request->all();
        $id = $result['topic_id'];
        $uid = $result['uid'];
        $topic = new topic();
        $topic_comment = new TopicComment();
        $topic_liek = new TopicLike();
        //帖子详情
        $data = $topic->where(['topic_id' => $id])->first()->toArray();

            //发帖人信息
            $userinfo = Topic::find($data['topic_id'])->user()->get()->toArray();
            $data['id'] = $userinfo[0]['id'];
            $data['name'] = $userinfo[0]['name'];
            $data['thumimg'] = $userinfo[0]['thumimg'];
            //帖子所属组信息
            $group = Group::find($data['topic_groupid']);
            $data['group_name'] = $group->group_name;

            //图片路径转换
            $data['topic_img'] = $this->url . $data['topic_img'];
            //帖子转换时间为多久之前
            $data['time'] = $this->formatDate($data['created_at']);
            //点赞数
            $data['like_count'] = $topic_liek->number($data['topic_id']);
            //评论数
            $data['comment_count'] = $topic_comment->number($data['topic_id']);
            //用户点赞状态
            $like = $topic_liek->is_like($data['topic_id'], $uid);
            if ($like) {
                $data['like'] = 1;
            } else {
                $data['like'] = 0;
            }
            //评论内容
            $data['comment'] = $topic_comment->where(['topicid'=>$data['topic_id']])->get()->toArray();
            foreach ($data['comment'] as $key => $value){
                //评论人信息
                $userinfo = TopicComment::find($value['uid'])->user()->get()->toArray();
                $data['comment'][$key]['user_id'] = $userinfo[0]['id'];
                $data['comment'][$key]['user_name'] = $userinfo[0]['name'];
                $data['comment'][$key]['thumimg'] = $userinfo[0]['thumimg'];
                //评论内容转换时间为多久之前
                $data['comment'][$key]['time'] = $this->formatDate($value['created_at']);
            }

        return $this->response($data);
    }
    //帖子点赞
    public function topic_like(Request $request){
        $topic_like = new TopicLike();
        $result = $request->all();
        //检查用户是否已经点赞
        $flag = $topic_like->is_like($result['topic_id'],$result['uid']);
        if($flag){
            //取消点赞
            $flag = $topic_like->cancel_like($result['topic_id'],$result['uid']);
            if($flag){return $this->response([],200,'取消点赞成功');}
        }else{
            //点赞
            $flag = $topic_like->like($result['topic_id'],$result['uid']);
            if($flag){return $this->response([],200,'点赞成功');}
        }
    }
    //帖子评论
    public function comment(Request $request){
        $result = $request->all();
        $topic_comment = new TopicComment();$users = new users();
        $topid = $result['topic_id'];$uid = $result['uid'];
        $contetn = $result['content'];
        if(($result['replyuid'] !== '') && ($result['replyid'] !== '')){
            //引用评论
            $replyuid = $result['replyuid'];$replyid = $result['replyid'];
            //根据被评论者的id找到被评论者的名字
            $name = $users->where(['id'=>$replyuid])->select('name')->first()->name;
            $flag = $topic_comment->add_comment($topid,$uid,$contetn,$name,$replyid);
        }else{
            //评论
            $flag = $topic_comment->add_comment($topid,$uid,$contetn);
        }
        if($flag){
            return $this->response([],200,'评论成功');
        }
    }

    //首页热门聚会
    public function party(Request $request){
        $activity = new Activity();
        $user_activity = new UserActivity();
        $activity_collect = new ActivityCollect();
        $receive = $request->all();
        $url = $this->url;
        $tag = new Tag();
        $tag = $tag->where(['tag_uid'=>$receive['uid']])->select('tag_content')->first()->toArray();
        //判断用户是否有选择分类
        if($tag['tag_content'] == ''){
            $data = $activity->where(['is_hot'=>1,'close'=>1])->orderBy('updated_at','desc')->get()->toArray();
            if($data){
                $data = $activity->activity($data,$receive['uid'],$url);
                return $this->response($data,200);
            }else{
                return $this->response([],404,'找不到有关的热门');
            }

        }else {
            //有选择分类查找相应分类下的热帖
            $categoryid = explode(',',$tag['tag_content']);
            foreach ($categoryid as $key => $value){
                $data[] = $activity->where(['activity_groupid'=>$value,'is_hot'=>1,'close'=>1])->get()->toArray();

            }
            //有选择分类的热门帖子
            if(count($data[0])){
                //合并数组
                $nin = [];
                foreach($data as $val){
                    foreach ($val as $k){
                        $nin[] = $k;
                    }
                }
                $nin = $activity->activity($nin,$receive['uid'],$url);
                /*foreach ($nin as $k => $v){
                    //参与聚会的成员
                    $userinfo = $user_activity->where(['m_activityid'=>$v['activity_id']])->get()->toArray();
                    foreach ($userinfo as $key => $value){
                        $nin[$k]['userimg'][] = UserActivity::find($value['id'])->user()->first()->thumimg;
                    }
                    //是否收藏
                    $result = $activity_collect->where(['uid'=>$receive['uid'],'activityid'=>$v['activity_id']])->first();
                    if($result){//已收藏
                        $nin[$k]['is_collect'] = 1;
                    }else{//未收藏
                        $nin[$k]['is_collect'] = 0;
                    }
                    //参与聚会的人数
                    //$nin[$k]['user_count'] = $user_activity->user_count($v['activity_id']);
                    $nin[$k]['start_time'] = date('n月j日 星期N',$v['start_time']);
                }*/
                return $this->response($nin,200);
            }else{
                //有选择分类但改分类下没有热门帖子
                $data = $activity->where(['is_hot'=>1,'close'=>1])->orderBy('updated_at','desc')->get()->toArray();
                if($data){
                    $data = $activity->activity($data,$receive['uid'],$url);
                    return $this->response($data,200);
                }else{
                    return $this->response([],404,'找不到有关的热门');
                }
            }

        }
        return $this->response($data,200);
    }
    //全部聚会
    public function all_party(Request $request){
        $receive = $request->all();
        $activity = new Activity();
        $url = $this->url;
        //热门活动
        $data = $activity->where(['close'=>1])->orderBy('is_hot','desc')->get()->toArray();
        if($data){
            //活动的参与人,是否收藏,
            $data = $activity->activity($data,$receive['uid'],$url);
            return $this->response($data,200);
        }else{
            return $this->response([],404,'找不到有关的活动');
        }

    }
    //聚会详情
    public function party_details(Request $request){
        $receive = $request->all();
        $id = $receive['activity_id'];
        $activity = new Activity();
        $user_activity = new UserActivity();
        $users = new Users();
        $group = new Group();
        $data = $activity->where(['activity_id'=>$id])->first()->toArray();
        if($data){
            //发起人信息
            $start_user = $users->where(['id'=>$data['uid']])->first();
            $data['user_name'] = $start_user->name;
            $data['user_img'] = $start_user->thumimg;
            //参与聚会的成员
            $userinfo = $user_activity->where(['m_activityid'=>$data['activity_id']])->get()->toArray();
            foreach ($userinfo as $key => $value){
                $data['userimg'][] = UserActivity::find($value['id'])->user()->first()->thumimg;
            }
            //聚会所属的组
            $groupinfo = $group->where(['group_id'=>$data['activity_groupid']])
                ->select('group_id','group_name')->first()->toArray();
            $data['group_id'] = $groupinfo['group_id'];
            $data['group_name'] = $groupinfo['group_name'];
            //活动进行状态
            $time = time();
            if($time < $data['start_time']){
                $data['state'] = '活动尚未开始';
            }elseif(($data['start_time'] < $time) && ($data['end_time'] > $time)){
                $data['state'] = '活动正在进行中';
            }elseif($time > $data['end_time']){
                $data['state'] = '活动已结束';
            }
            //时间戳转换
            $data['start_time'] = date('m月d日 周N H:i',$data['start_time']);
            $data['end_time'] = date('H:i',$data['end_time']);

            return $this->response($data,200);
        }else{
            return $this->response([],404,'找不到有关的热门');
        }

    }
    //报名参与聚会
    public function add_party(Request $request){
        $user_activity = new UserActivity();
        $activity = new Activity();
        $user_group = new UserGroup();
        $receive = $request->all();
        //查询聚会所属的组ID
        $groupid = $activity->where(['activity_id'=>$receive['activity_id']])
            ->select('activity_groupid')->first()->activity_groupid;
        //检查用户是否加入该聚会的组
        $group_flag = $user_group->where(['groupid'=>$groupid,'uid'=>$receive['uid']])->first();
        if($group_flag){
            //查询是否已经报名
            $group_flag = $user_activity->where(['m_uid'=>$receive['uid'],'m_activityid'=>$receive['activity_id']])->first();
            if($group_flag){
                return $this->response([],200,'已经报名,请勿重复提交');
            }
            //开始报名
            $flag = $user_activity->create(['m_uid'=>$receive['uid'],'m_activityid'=>$receive['activity_id']]);
            if($flag){
                return $this->response([],200,'报名成功');
            }else{
                return $this->response([],200,'请稍后再试');
            }
        }else{
            return $this->response([],202,'尚未加入该组');
        }

    }
    //收藏聚会
    public function collect(Request $request){
        $collect = new ActivityCollect();
        $receive  = $request->all();
        //查询是否已经收藏
        $flag = $collect->where(['uid'=>$receive['uid'],'activityid'=>$receive['activity_id']])->first();
        if($flag){
            //若已收藏,则取消收藏
            $flag = $collect->where(['uid'=>$receive['uid'],'activityid'=>$receive['activity_id']])->forceDelete();
            if($flag){
                return $this->response([],200,'取消收藏成功');
            }
        }else{
            //若为收藏,则收藏
            $flag = $collect->create(['uid'=>$receive['uid'],'activityid'=>$receive['activity_id']]);
            if($flag){return $this->response([],200,'收藏成功');}
        }
    }
    //搜索
    public function search(Request $request){
        $activity = new Activity();
        $topic = new Topic();
        $group = new Group();
        $receive = $request->all();
        $val = $receive['val'];
        $uid = $receive['uid'];
        $url = $this->url;
        if($receive){
            //搜索活动
            $result['activity'] = $activity->where('activity_name','like','%' . $val . '%')
            ->orWhere('details','like','%' . $val . '%')->get()->toArray();
            //搜索帖子
            $result['topic'] = $topic->where('topic_name','like','%' . $val . '%')
            ->orWhere('details','like','%' . $val . '%')->get()->toArray();
            //搜索分组
            $result['group'] = $group->where('group_name','like','%' . $val . '%')
                ->orWhere('group_content','like','%' . $val . '%')->get()->toArray();
        }
        if(count($result['activity']) || count($result['topic']) || count($result['group'])){
            //搜索到的活动详情
            if(count($result['activity'])){
                $data['activity'] = $activity->activity($result['activity'],$uid,$url);
            }else{
                $data['activity'] = [];
            }
            //搜索到的帖子详情
            if(count($result['topic'])){
                $data['topic'] = $topic->topic_details($result['topic'],$url);
            }else{
                $data['topic'] = [];
            }
            //搜索到的分组详情
            if(count($result['group'])){
                $data['group'] = $group->group_details($result['group'],$uid,$url);
            }else{
                $data['group'] = [];
            }
            return $this->response($data);
        }else{
            return $this->response([],200,'没有相关的内容');
        }

    }

}
